from django.shortcuts import render,redirect
from app01 import models
from app01.utils.pagination import Pagination


def depart_list(request):
    """部门列表"""
    #数据库获取部门信息
    depart_list=models.Department.objects.all()
    return  render(request, 'depart_list.html',{'depart_list':depart_list})

def depart_add(request):
    """添加部门"""
    if request.method == 'GET':
        return render(request, 'depart_add.html')
    # 获取用户提交的数据
    title=request.POST.get('title')
    # 保存到数据库
    models.Department.objects.create(title=title)
    # 重订向跳转到部门列表页面
    return redirect('/depart/list/')

def depart_delete(request):
    """删除部门"""
    # 获取页面部门的id
    nid=request.GET.get('nid')
    # 删除部门
    models.Department.objects.filter(id=nid).delete()
    # 重订向跳转到部门列表页面
    return redirect('/depart/list/')

def depart_edit(request,nid):
    """修改部门"""
    if request.method == 'GET':
        #根据ID获取数据
        row_object=models.Department.objects.filter(id=nid).first()
    title=request.POST.get('title')

    # 根据nid修改标题
    models.Department.objects.filter(id=nid).update(title=title)

    # 重订向跳转到部门列表页面
    return redirect('/depart/list/')

def user_list(request):
    """用户列表"""
    #获取用户列表
    queryset=models.UserInfo.objects.all()
    page_object = Pagination(request, queryset)
    context = {
        "queryset": page_object.page_queryset,  # 分页数据
        "page_string": page_object.html()  # 生成页码
    }

    return render(request, 'user_list.html',context)